package com.haoshuang.zhouzhoubang.ui.page.welfare.insure.record

import android.content.Context
import android.os.Bundle
import android.widget.ImageView
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityInsureApplyRecordBinding
import com.haoshuang.zhouzhoubang.model.bean.InsureApplyRecordBean
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical

/**
 *  保险申请记录
 */
class InsureApplyRecordActivity : BaseVMActivity<InsureApplyRecordViewModel, ActivityInsureApplyRecordBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<InsureApplyRecordActivity>()
        }
    }

    private val data = arrayListOf<InsureApplyRecordBean>()
    private val adapter = InsureApplyRecordAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply{
            titleBar.title = "申请记录"
            titleBar.leftImageView().click { finish() }

            recyclerView.vertical().adapter = adapter
            adapter.setEmptyData()
            adapter.setOnItemChildClick(id = R.id.ivIDCard) { view, position, item ->
                XPopup.Builder(mActivity)
                    .asImageViewer(view as ImageView, item?.card_front, SmartGlideImageLoader())
                    .show()
            }
            adapter.setOnItemChildClick(id = R.id.ivIDCardBack) { view, position, item ->
                XPopup.Builder(mActivity)
                    .asImageViewer(view as ImageView, item?.card_side, SmartGlideImageLoader())
                    .show()
            }

            refreshLayout.setOnRefreshListener {
                p = 1
                isShowing = false
                vm.getInsureApplyRecord(p)
            }

            refreshLayout.setOnLoadMoreListener {
                p++
                isShowing = false
                vm.getInsureApplyRecord(p)
            }

            refreshLayout.autoRefresh()
        }
    }

    override fun initData() {
    }

    override fun startObserve() {
        vm.getInsureApplyRecordData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, data, adapter, p)
            }
        }
    }

}